package de.slothsoft.wasbinich.swingclient.common;

import java.awt.GridLayout;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import de.slothsoft.wasbinich.swingclient.res.Messages;

public class SocialControl extends JPanel {

    private static final long serialVersionUID = 5735488515932045399L;

    private static final String FACEBOOK_TITLE = "Facebook";
    private static final String FACEBOOK_URL = "https://www.facebook.com/sharer/sharer.php?u={0}";

    private static final String GOOGLE_TITLE = "Google+";
    private static final String GOOGLE_URL = "https://plus.google.com/share?url={0}";

    public SocialControl(String content) {
	setLayout(new GridLayout(2, 1));
	setOpaque(false);
	createControls(content);
    }

    private void createControls(String content) {
	String encodedContent;
	try {
	    encodedContent = URLEncoder.encode(content, "ISO-8859-1");
	} catch (UnsupportedEncodingException e) {
	    throw new RuntimeException(e);
	}
	createLink(FACEBOOK_TITLE, FACEBOOK_URL, encodedContent);
	createLink(GOOGLE_TITLE, GOOGLE_URL, encodedContent);
    }

    private void createLink(String title, String urlPattern, String encodedContent) {
	JLabel link = SwingUtil.createLink(MessageFormat.format(Messages.getString("SocialControl.share"), title),
		MessageFormat.format(urlPattern, encodedContent));
	link.setHorizontalAlignment(SwingConstants.CENTER);
	add(link);
    }

}
